import { pipeRegExp } from "../utils/form";

export const requireRule = {
  required: true,
  message: "请填入内容",
  trigger: "blur",
};

export function getRequireRule(message = "请填入内容") {
  return {
    required: true,
    message,
    trigger: "blur",
  };
}

export const lengthRule = {
  min: 5,
  max: 13,
  message: "长度范围 5 - 13",
  trigger: "blur",
};

export function getLengthRule(min, max = 99999) {
  return {
    min,
    max,
    message: max === 99999 ? `最小长度是${min}` : `长度范围在${min} - ${max}`,
    trigger: "blur",
  };
}

export function getPasswordRule() {
  return {
    validator: (rule, value, callback) => {
      const reg1 = /[a-zA-Z]+/g;
      const reg2 = /[\d]+/g;
      const reg3 = /[\-\+\*\#]+/g;
      if (value === "") {
        callback(new Error("Please input the password"));
      } else if (!pipeRegExp([reg1, reg2, reg3], value)) {
        callback(new Error("密码必须包含字母、数字和字符- + * #"));
      } else {
        callback();
      }
    },
    trigger: "blur",
    message: "密码必须包含字母、数字和字符- + * #",
  };
}

export const userLengthRule = {
  validator: (rule, value, callback) => {
    if (value.length === 11) {
      callback();
      return;
    }
    callback(new Error("账号长度11位"));
  },
  trigger: "blur",
  message: "账号长度11位",
};

const phoneReg = /0?(13|14|15|17|18)[0-9]{9}/;

export const phoneRule = {
  validator: (rule, value, callback) => {
    if (phoneReg.test(value)) {
      callback();
      return;
    }
    callback(new Error("账号应为手机号格式"));
  },
  trigger: "blur",
  message: "账号应为手机号格式",
};

export const commonPasswordLengthRule = getLengthRule(6, 18);
